using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using UMEVOR.DTO;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace UMEVOR.DAL
{
    public class DALT_TELA : DALConnection
    {

        #region Incluir

        public void incluir(T_TELA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" INSERT INTO T_TELA ");
                SQL.Append("    (ID_TELA, CD_ITEM_MENU, TX_DSCR_TELA) ");
                SQL.Append(" VALUES ");
                SQL.Append("    (:ID_TELA, :CD_ITEM_MENU, :TX_DSCR_TELA) ");

                DbCommand insertT_TELA = database.GetSqlStringCommand(SQL.ToString());

                database.AddInParameter(insertT_TELA, ":ID_TELA", DbType.Int32, obj.ID_TELA);
                database.AddInParameter(insertT_TELA, ":CD_ITEM_MENU", DbType.String, obj.CD_ITEM_MENU);
                database.AddInParameter(insertT_TELA, ":TX_DSCR_TELA", DbType.String, obj.TX_DSCR_TELA);

                insertT_TELA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(insertT_TELA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Incluir[]

        public void incluir(T_TELA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] insertT_TELA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_TELA o = obj[index];
                    SQL = new StringBuilder();

                    SQL.Append(" INSERT INTO T_TELA ");
                    SQL.Append("    (ID_TELA, CD_ITEM_MENU, TX_DSCR_TELA) ");
                    SQL.Append(" VALUES ");
                    SQL.Append("    (:ID_TELA, :CD_ITEM_MENU, :TX_DSCR_TELA) ");

                    insertT_TELA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(insertT_TELA[index], ":ID_TELA", DbType.Int32, o.ID_TELA);
                    database.AddInParameter(insertT_TELA[index], ":CD_ITEM_MENU", DbType.String, o.CD_ITEM_MENU);
                    database.AddInParameter(insertT_TELA[index], ":TX_DSCR_TELA", DbType.String, o.TX_DSCR_TELA);

                    insertT_TELA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in insertT_TELA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar

        public void alterar(T_TELA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" UPDATE T_TELA SET ");
                SQL.Append("    CD_ITEM_MENU = :CD_ITEM_MENU , ");
                SQL.Append("    TX_DSCR_TELA = :TX_DSCR_TELA ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_TELA = :ID_TELA  ");

                DbCommand updateT_TELA = database.GetSqlStringCommand(SQL.ToString());

                database.AddInParameter(updateT_TELA, ":ID_TELA", DbType.Int32, obj.ID_TELA);
                database.AddInParameter(updateT_TELA, ":CD_ITEM_MENU", DbType.String, obj.CD_ITEM_MENU);
                database.AddInParameter(updateT_TELA, ":TX_DSCR_TELA", DbType.String, obj.TX_DSCR_TELA);

                updateT_TELA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(updateT_TELA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar[]

        public void alterar(T_TELA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] updateT_TELA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_TELA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" UPDATE T_TELA SET ");
                    SQL.Append("    CD_ITEM_MENU = :CD_ITEM_MENU , ");
                    SQL.Append("    TX_DSCR_TELA = :TX_DSCR_TELA ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_TELA = :ID_TELA  ");

                    updateT_TELA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(updateT_TELA[index], ":ID_TELA", DbType.Int32, o.ID_TELA);
                    database.AddInParameter(updateT_TELA[index], ":CD_ITEM_MENU", DbType.String, o.CD_ITEM_MENU);
                    database.AddInParameter(updateT_TELA[index], ":TX_DSCR_TELA", DbType.String, o.TX_DSCR_TELA);

                    updateT_TELA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in updateT_TELA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir

        public void excluir(T_TELA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" DELETE FROM T_TELA ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_TELA = :ID_TELA  ");

                DbCommand deleteT_TELA = database.GetSqlStringCommand(SQL.ToString());

                database.AddInParameter(deleteT_TELA, ":ID_TELA", DbType.Int32, obj.ID_TELA);

                deleteT_TELA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(deleteT_TELA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir[]

        public void excluir(T_TELA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] deleteT_TELA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_TELA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" DELETE FROM T_TELA ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_TELA = :ID_TELA  ");

                    deleteT_TELA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(deleteT_TELA[index], ":ID_TELA", DbType.Int32, o.ID_TELA);

                    deleteT_TELA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in deleteT_TELA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ObterPorPK

        public T_TELA obterPorPK(int pID_TELA)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_TELA ");
                SQL.Append(" WHERE ");
                SQL.Append("  ID_TELA = :ID_TELA  ");

                DbCommand consultaT_TELA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_TELA.CommandText = SQL.ToString();

                database.AddInParameter(consultaT_TELA, ":ID_TELA", DbType.Int32, pID_TELA);

                IDataReader reader = database.ExecuteReader(consultaT_TELA, DALConnection.transaction);
                T_TELA obj = null;

                if (reader.Read())
                {
                    obj = new T_TELA();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_TELA")))
                    {
                        obj.ID_TELA = Int32.Parse(reader["ID_TELA"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("CD_ITEM_MENU")))
                    {
                        obj.CD_ITEM_MENU = reader["CD_ITEM_MENU"].ToString();
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DSCR_TELA")))
                    {
                        obj.TX_DSCR_TELA = reader["TX_DSCR_TELA"].ToString();
                    }
                }
                reader.Dispose();
                consultaT_TELA.Dispose();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ListarTodos

        public List<T_TELA> listarTodos()
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_TELA ORDER BY TX_DSCR_TELA");

                DbCommand consultaT_TELA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_TELA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_TELA, DALConnection.transaction);
                List<T_TELA> lista = new List<T_TELA>();
                T_TELA obj = null;

                while (reader.Read())
                {
                    obj = new T_TELA();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_TELA")))
                    {
                        obj.ID_TELA = Int32.Parse(reader["ID_TELA"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("CD_ITEM_MENU")))
                    {
                        obj.CD_ITEM_MENU = reader["CD_ITEM_MENU"].ToString();
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DSCR_TELA")))
                    {
                        obj.TX_DSCR_TELA = reader["TX_DSCR_TELA"].ToString();
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_TELA.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

    }
}

